import requests
from lxml import etree
import pymongo
import pymysql



url = 'https://quotes.toscrape.com/page/1/'



class ToScrape:
	def __init__(self,url):
		self.url = url
	
	
	def get_data(self):
		res = requests.get(self.url)
		tree = etree.HTML(res.text)
		datas = tree.xpath('//div[@class="row"]//div[@class="quote"]')
		data_value = []
		for data in datas:
			text = data.xpath('.//span[@class="text"]/text()')[0][1:-1]
			author = data.xpath('.//small[@class="author"]/text()')[0]
			tags = data.xpath('.//div[@class="tags"]/a/text()')
			data_value.append({'text': text, 'author': author, 'tags': tags})
		return data_value
	
	def save_mongodb(self):
		data_value = self.get_data()
		client = pymongo.MongoClient('localhost', 27017)
		db = client['toscrape']
		collection = db['quotes']
		collection.insert_many(data_value)
		print('保存成功')
	
	def save_mysql(self):
		data_value = self.get_data()
		db = pymysql.connect(host='localhost', user='toscrape', password='123456', db='toscrape', charset='utf8')
		cursor = db.cursor()
	


if __name__ == '__main__':
	toscrape = ToScrape(url)
	# toscrape.save_mongodb()
	
	